<?php

namespace Product\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Product\Model\Product;
use Product\Form\ProductForm;
use Zend\Db\TableGateway\TableGateway;

class ProductController extends AbstractActionController {
	protected $dbAdapter;
	protected $productTable;
	public function indexAction() {
		return new ViewModel ( array (
				'products' => $this->getProductTable ()->fetchAll ()
		) );
	}
	public function addAction() {
		$sl = $this->getServiceLocator ();
		$config = $sl->get ( "Config" );

		$form = new ProductForm ();
		$form->get ( 'submit' )->setValue ( 'Add Product' );
		$form->get ( 'category' )->setValueOptions ( $this->getAllCategory () );

		$request = $this->getRequest ();

		if ($request->isPost ()) {
			$noFile = $request->getPost ()->toArray ();
			$file = $this->params ()->fromFiles ( 'preview_img' );
			$data = array_merge ( $noFile, array (
					'preview_img' => $file
			) );

			$product = new Product ();

			$product->setUploadPath ( $config ['uploadFolders'] ['productImage'] );
			$form->setInputFilter ( $product->getInputFilter () );
			$form->setData ( $data );
			if ($form->isValid ()) {
				$product->exchangeArray ( $form->getData () );
				if (! empty ( $file ['name'] )) {
					$product->preview_img = $file ['name'];
				} else {
					$product->preview_img = null;
				}
				$this->getProductTable ()->saveProduct ( $product );
				return $this->redirect ()->toRoute ( 'product' );
			}
		}
		return array (
				'form' => $form
		);
	}
	public function editAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'product', array (
					'action' => 'add'
			) );
		}
		try {
			/* @var $product \Product\Model\Product */
			$product = $this->getProductTable()->getProduct ( $id );
			$product->category = explode ( '|', $product->category );
			$old_img = $product->preview_img;
		} catch ( \Exception $ex ) {
			return $this->redirect ()->toRoute ( 'product', array (
					'action' => 'index'
			) );
		}

		$form = new ProductForm ();

		$sl = $this->getServiceLocator ();
		$config = $sl->get ( "Config" );
		$product->setUploadPath ( $config ['uploadFolders'] ['productImage'] );

		$form->bind ( $product );
		$form->get( 'submit' )->setValue ( 'Save Change' );
		$form->get( 'category' )->setValueOptions ( $this->getAllCategory () );

		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$request = $this->getRequest ();
			$noFile = $request->getPost ()->toArray ();
			$file = $this->params ()->fromFiles ( 'preview_img' );

			$data = array_merge ( $noFile, array (
					'preview_img' => $file
			) );

			$form->setInputFilter($product->getInputFilter());
			$form->setData ( $data );
			if ($form->isValid ()) {
				$product->exchangeArray($noFile);
				if (! empty ( $file ['name'] )) {
					$product->preview_img = $file ['name'];
				} else {
					$product->preview_img = $old_img;
				}
				$sl = $this->getServiceLocator();
				/* @var $dbAdapter \Zend\Db\Adapter\Adapter */
				$dbAdapter = $sl->get("Zend\Db\Adapter\Adapter");
				/* @var $connection \Zend\Db\Adapter\Driver\ConnectionInterface */
				$connection = $dbAdapter->getDriver()->getConnection();//
				try {
					//start transaction
					$connection->beginTransaction();
					$this->getProductTable()->saveProduct($product);
					throw new \Exception("asdf");
					$this->getProductTable()->saveProduct($product);
					//commit
					$connection->commit();
				} catch (\Exception $e) {
					$connection->rollback();
					//rollback
				}

				return $this->redirect ()->toRoute ( 'product' );
			}
		}

		return array (
				'id' => $id,
				'form' => $form,
				'preview_imgPath' => $product->getUploadPath ()
		);
	}
	public function deleteAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'product' );
		}

		$request = $this->getRequest ();

		if ($request->isPost ()) {
			$del = $request->getPost ( 'del', 'No' );

			if ($del == 'Yes') {
				$id = ( int ) $request->getPost ( 'id' );
				$this->getProductTable ()->deleteProduct ( $id );
			}
			return $this->redirect ()->toRoute ( 'product' );
		}

		return array (
				'id' => $id,
				'product' => $this->getProductTable ()->getProduct ( $id )
		);
	}
	public function detailAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			$this->redirect ()->toRoute ( 'product' );
		}
		try {
			$product = $this->getProductTable ()->getProduct ( $id );

			return array (
					'product' => $product
			);
		} catch ( \Exception $ex ) {
			return $this->redirect ()->toRoute ( 'product' );
		}
	}
	public function getAllCategory() {
		$sm = $this->getServiceLocator ();
		$dbAdapter = $sm->get ( 'Zend\Db\Adapter\Adapter' );
		$CategoryTableGateway = new TableGateway ( 'category', $dbAdapter );
		$allCategory = $CategoryTableGateway->select ();

		$arrCategory = array ();

		foreach ( $allCategory as $category ) {
			$arrCategory [$category->id] = $category->name;
		}

		return $arrCategory;
	}
	public function getProductTable() {
		if (! $this->productTable) {
			$sm = $this->getServiceLocator ();
			$this->productTable = $sm->get ( 'Product\Model\ProductTable' );
		}
		return $this->productTable;
	}
}
